{% include 'JDXJ/JDXJ_index.html' %}


<div class="am-cf admin-main">
  {% include 'sidebar.html' %}
  <!-- content start -->
  <div class="admin-content">
    <div class="admin-content-body">
      <div class="am-cf am-padding">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">日常巡查</strong></div>
      </div>
      <div class="am-g" style="position: absolute;height: 100%;right: 0px;left: 260px;width: auto;">

        {% include 'JDXJ/RCXC/JDXJ_RCXC_SIDEBAR.html' %}
        
        <div class="am-u-sm-10" style="position: absolute;width:auto;right:0;left: 200px;overflow-y: scroll;bottom: 200px;top: 0;">
          <div class="am-u-sm-12 am-u-md-6">
            <div class="am-btn-toolbar">
              <div class="am-btn-group am-btn-group-xs">
                <button  type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="back_to()">返回</button>
              </div>
            </div>
          </div>

          <div class="am-form-group" style="margin-top: 60px">
            <div class="am-g am-margin-top am-form-group" style="padding-left: 100px;">
              <label>基本信息</label>         
            </div>
            <div class="am-g am-margin-top am-form-group">
              <div class="am-u-sm-5 am-u-md-2 am-text-right">地理位置：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <select  id='location' class="am-u-md-12"  data-am-selected="{searchBox: 1}" ">
                  {% for i in locationInfo %}
                  <option value={{ i.location_id }} >{{ i.location }}</option> 
                  {% endfor %}
                </select>

              </div>
              <div class="am-u-sm-5 am-u-md-2 am-text-right">系统：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <select  id='sysname' class="am-u-md-10"  data-am-selected="{searchBox: 1}"  onchange="sys_change()">
                {% for i in sysInfo %}
                  <option value={{ i.sys_id }} >{{ i.sysname }}</option> 
                  {% endfor %}
                </select>
              </div>          
            </div>

            <div class="am-g am-margin-top am-form-group">
              <div class="am-u-sm-5 am-u-md-2 am-text-right">设备类别：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <select  id='devicetype_id' class="am-u-md-12"  data-am-selected="{searchBox: 1}" onchange="devicetype_change()">
                
                </select>
              </div>
              <div class="am-u-sm-5 am-u-md-2 am-text-right">设备名称：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <select  id='device_id' class="am-u-md-12"  data-am-selected="{searchBox: 1}" >
                
                </select>
              </div>
                      
            </div>

            <div class="am-g am-margin-top am-form-group">
              <div class="am-u-sm-5 am-u-md-2 am-text-right">检查项目：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <select  id='jcpro'  class="am-u-md-10"  data-am-selected="{searchBox: 1}" onchange="jcpro_change()">
                
                </select>
              </div>  
              <div class="am-u-sm-5 am-u-md-2 am-text-right">检查内容：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end"  >
                <select  id='jccontent' class="am-u-md-12"  data-am-selected="{searchBox: 1}">
               
                </select>

              </div>
              
                      
            </div>
            <div class="am-g am-margin-top am-form-group">
              <div class="am-u-sm-5 am-u-md-2 am-text-right">作业完成时间：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <input type="date" class="input_width" id="checkdate"/>
              </div>  
              <div class="am-u-sm-5 am-u-md-2 am-text-right">作业人：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end"  >
                  <select  id='user_id' name='user_id' class="input_width" required="">
                  {% for i in userInfo %}
                  {% if i.user_id == user_id %}
                  <option value={{ i.user_id }} selected="selected">{{ i.real_name }}</option> 
                  {% else %}
                  <option value={{ i.user_id }} >{{ i.real_name }}</option>
                  {% endif %}
                  {% endfor %}
                </select>

              </div>
              
                      
            </div>
            <div class="am-g am-margin-top am-form-group">
              <div class="am-u-sm-5 am-u-md-2 am-text-right">备注：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <textarea rows="4" class="am-u-md-10" id="remark"></textarea>
              </div>
              <div class="am-u-sm-5 am-u-md-2 am-text-right">媒体信息：</div>
              <div class="am-u-sm-7 am-u-md-3 am-u-end" >
                <input type="file" id="file_choose" style="display: none;" >
                <button  type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="add_img()">添加</button>
                <input type="hidden" id="img_id">
              </div>
              
          </div>

            <br>
            <div class="am-g am-margin-top am-form-group">

              <div style="margin: 5px 10%;" id="img_ul" >


              
              </div>

            </div>
          </div>
          <br>

          <div class="am-u-sm-12 am-u-md-6">
            <div class="am-btn-toolbar ">
              <div class="am-btn-group am-btn-group-xs"  style="float: right;">
                <button  type="button" class="am-btn am-btn-primary btn-loading-example" onclick="save_add()">保存</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<!--[if lt IE 9]>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.staticfile.org/modernizr/2.8.3/modernizr.js"></script>
<script src="assets/js/amazeui.ie8polyfill.min.js"></script>
<![endif]-->

<!--[if (gte IE 9)|!(IE)]><!-->
<script src="/static/assets/js/jquery.min.js"></script>
<!--<![endif]-->
<script src="/static/assets/js/amazeui.min.js"></script>
<script src="/static/assets/js/amazeui.tree.js"></script>
<script type="text/javascript" src="/static/assets/js//layer.js"></script>
<script src="/static/assets/datatables/amazeui.datatables.min.js"></script>
<script src="/static/assets/dialog/amazeui.dialog.min.js"></script>
<script src="/static/assets/js/amazeui.chosen.min.js"></script>
<link rel="stylesheet" type="text/css" href="/static/assets/css/amazeui.tree.css" />
<script src="/static/assets/js/app.js"></script>
<style type="text/css">
  .li_a {
    color: #5c5c5c;
  }
  .imgDiv {
            display: inline-block;
            position: relative;
            float: left;
        }
 
        .imgDiv .delete {
          background:url("/static/assets/i/close.png");
            position: absolute;
            top: 0px;
            right: 0px;
            width: 17px;
            height: 17px;
            display: none;
        }
</style>

 <script type="text/javascript">

  $("#collapse-nav8").addClass("am-in");
  $("#jdxj").removeClass("am-collapsed");
  $("#jdxj").css("color", "#3399CC");
  $("#jdxjrcxc").css("color", "#3399CC");
  $("#rcwhjl").css("color", "#3399CC");
  $("#jdxjrcxc").removeClass("li_a");
  
  // var tp = {{ type|safe }};
  // if(tp == "1"){
  //   $("#xjywOdqjc").removeClass("li_a");
  // }else if(tp == "2"){
  //   $("#xjywOjcjc").removeClass("li_a");
  // }

    $.ajaxSetup({
        data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
      });
    $(document).ready(function(){
      var date = new Date();
      var seperator1 = "-";
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
         month = "0" + month;
       }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
     }
     var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
   
     $("#checkdate").attr('value',currentdate); 
     init();
    })
  // $(document).ready(function(){

  //   // parts_change();
  //   // disease_change();
  //   init();

  // })

  // var bridge_id = {{ bridge_id|safe }};
  // var check_id = {{ check_id|safe }};

  function back_to(){
    window.history.back(-1);
  }

  // function parts_change(){
  //   print_id('','','');
  //   var parts_code = $("#parts_code option:selected").val();
  //   $.ajax({
  //     "url":"/QLXJ_XJYW_DQJC_SEE_DATA_MEMBER/?check_id="+check_id+"&bridge_id="+bridge_id+"&parts_code="+parts_code+"",
  //     "datatype":"json",
  //     "type":"get",
  //     "async":"false"
  //   }).success(function(data){
  //     if (data.status == 1) {
  //       $("#bridge_member").empty();
  //       for(var i=0;i<data.data.length;i++){
  //         $('#bridge_member').append('<option value='+data.data[i].bridge_member_id+' >'+data.data[i].member_name+'</option>');
  //       } 
  //     } else {
  //       AlertMSG(data.message)
  //     }
  //   }).error(function(data){
  //     AlertMSG("查询构件信息失败！")
  //   });
  // }


  function sys_change(){
    // print_id('','','');
    var bridge_member_id = $("#sysname option:selected").val();
    if(bridge_member_id != undefined){
    $.ajax({
      "url":"/QLXJ_XJYW_DQJC_SEE_ADD_SYS/?sys_id="+bridge_member_id+"",
      "datatype":"json",
      "type":"get",
      "async":"false"
    }).success(function(data){
      if (data.status == 1) {
        $("#devicetype_id").empty();
        for(var i=0;i<data.data.length;i++){
          $('#devicetype_id').append('<option value="'+data.data[i].devicetype_id+'" >'+data.data[i].devicetype_name+'</option>');
        } 
      } else {
        AlertMSG(data.message)
      }
    }).error(function(data){
      AlertMSG("查询设备类别失败！")
    });
    }
  }

  function devicetype_change(){
    // print_id('','','');
    var disease_id = $("#devicetype_id option:selected").val();
    var location_id = $("#location option:selected").val();
    $.ajax({
      "url":"/QLXJ_XJYW_DQJC_SEE_ADD_DEVICETYPE/?devicetype_id="+disease_id+"&location_id="+location_id+"",
      "datatype":"json",
      "type":"get",
      "async":"false"
    }).success(function(data){
      if (data.status == 1) {
        $("#device_id").empty();
        for(var i=0;i<data.data.devicelist.length;i++){
          $('#device_id').append('<option value="'+data.data.devicelist[i].device_id+'" >'+data.data.devicelist[i].device_name+'</option>');
        };
        $("#jcpro").empty();
        for(var j=0;j<data.data.JCPROInfo.length;j++){
          $('#jcpro').append('<option value="'+data.data.JCPROInfo[j].pro_id+'" >'+data.data.JCPROInfo[j].pro_name+'</option>');
        } 
      } else {
        AlertMSG(data.message)
      }
    }).error(function(data){
      AlertMSG("查询设备信息或检查项目信息失败！")
    });
  }
  function jcpro_change(){
    // print_id('','','');
    var jcpro_id = $("#jcpro option:selected").val();
    if(jcpro_id != undefined){
    $.ajax({
      "url":"/QLXJ_XJYW_DQJC_SEE_ADD_Checkitem/?jcpro_id="+jcpro_id+"",
      "datatype":"json",
      "type":"get",
      "async":"false"
    }).success(function(data){
      if (data.status == 1) {
        $("#jccontent").empty();
        for(var i=0;i<data.data.length;i++){
          $('#jccontent').append('<option value="'+data.data[i].content_id+'" >'+data.data[i].content_name+'</option>');
        } 
      } else {
        AlertMSG(data.message)
      }
    }).error(function(data){
      AlertMSG("查询检查内容失败！")
    });
    }
  }

  // function scale_change(){
  //   var disease_scale_id = $("#disease_scale_id option:selected").val();
  //   if(disease_scale_id != undefined){
  //     var number = disease_scale_id.indexOf(",");
  //     if(number >= 0){
  //       $('#description').html(disease_scale_id.substring(number+1));
  //     }else{
  //       $('#description').html();
  //     }
  //   }else{
  //     $('#description').html();
  //   }
  // }

  // function disease_position(){
  //   var disease_id = $("#disease_id option:selected").val();
  //   var member_id = $("#bridge_member option:selected").val();
  //   if (disease_id != undefined && member_id != undefined){
  //     popup("病害位置","/QLXJ_XJYW_DQJC_DISEASE_POSITION/?disease_id="+disease_id+"&member_id="+member_id+"");
  //   }else{
  //     AlertMSG("请选择构件编号及病害类型！");
  //   }
    
  // }

  // function print_id(data,message1,message2){
  //   // console.log(data1);
  //   // console.log(data2);
  //   $('#position').html(message1);
  //   $('#description_des').html(message2);
  //   $('#data_past').val(data);

  // }


  function add_img(){
    $("#file_choose").click();
  }


        function init() {
            $(".imgDiv").mouseenter(function () {
                $(this).find(".delete").show();
 
            }); 
            $(".imgDiv").mouseleave(function () {
                $(this).find(".delete").hide();
            });
            
            $(".delete").on("click",function(){

              $(this).parent().remove();

            })
 
        }

  var upimg = document.querySelector('#file_choose');
  upimg.addEventListener('change', function(e) {
    var files = this.files;
    if(files.length) {
      if(checkFile(files)) {
        uploadFiles(files);
      }
    }
  });

  function checkFile(files) {
    if(files.length == 1) {
      //console.log(files[0].type);
      // var aaa = ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif)$
      if(!files[0].type.match(/.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif/)) { //判断上传文件格式
        AlertMSG('上传的文件格式不正确')
        return false;
      }
      return true;
    } else {
      AlertMSG('只能上传一个文件')
      return false;
    }
  }

  var uploadFiles = function(files) {
    var form_data = new FormData();
    form_data.append("myfile", files[0]);
    // 提交ajax的请求
    $.ajax({
        url: "/QLXJ_XJYW_DQJC_ADD_UPLOADFILE/",
        type: 'POST',
        data: form_data,
        processData: false, // tell jquery not to process the data
        contentType: false, // tell jquery not to set contentType
        success: function(result) {
          //console.log(result.data)
          if(result.status == 1) {
            alert("上传成功！");
            // console.log(result)
            $("#file_choose").val(""); 
            $('#img_ul').append('<div class="imgDiv" ><img src="'+result.data[0].file_url+'"  style="max-width: 500px;padding: 5px;"/><div class="delete" attr1="'+result.data[0].file_name+'"  attr2="'+result.data[0].file_url+'"></div></div>');
            init();

          } else {
            AlertMSG(result.message)
          }

        }
      
    });
  }

function insertTitle(path){  
   var test1 = path.lastIndexOf("/");  //对路径进行截取
   var test2 = path.lastIndexOf("\\");  //对路径进行截取
   var test= Math.max(test1, test2)
   if(test<0){  
     document.getElementById("file_name").value = path;
   }else{
    document.getElementById("file_name").value = path.substring(test + 1); //赋值文件名
   }  
}  

function check(id){
  $('#'+id).click();
}

function save_add(){
  var location = $("#location option:selected").val();
  var sys_id = $("#sysname option:selected").val();
  var devicetype_id = $("#devicetype_id option:selected").val();
  var device_id = $("#device_id option:selected").val();
  var jcpro = $("#jcpro option:selected").val();
  var jccontent = $("#jccontent option:selected").val();
  var remark = $("#remark").val();
  var user_id = $("#user_id option:selected").val();
  var checkdate = $("#checkdate").val();


  // var device_id = $("#device_id").html();
  // var description = $("#description_des").html();
  // var attrList = $("#data_past").val();
  var photoList = get_photo();
  
  

  if(sys_id == '' || sys_id == undefined){
    AlertMSG("请选择系统！")
  }else if (location == '' || location == undefined){
    AlertMSG("请选择地理位置！")
  }else if (devicetype_id == '' || devicetype_id == undefined){
    AlertMSG("请选择设备类别！")
  }else if (device_id == '' || device_id == undefined){
    AlertMSG("请选择设备！")
  }else if (jcpro == '' || jcpro == undefined) {
    AlertMSG("请选择检查项目！")
  }else if (jccontent == '' || jccontent == undefined) {
    AlertMSG("请选择检查内容！")
  } else if (remark == '' || remark == undefined) {
    AlertMSG("请填写备注信息！")
  } else if (user_id == '' || user_id == undefined) {
    AlertMSG("请选择作业人！")
  } else if (checkdate == '' || checkdate == undefined) {
    AlertMSG("请选择作业完成时间")
  } else {
    $.ajax({
      "url":"/JDXJ_RCXC_WHJL_ADDSAVE/",
      "datatype":"json",
      "data":{"location_id":location,"sys_id":sys_id,"devicetype_id":devicetype_id,"device_id":device_id,"pro_id":jcpro,"content_id":jccontent,"photoList":photoList,"remark":remark,"user_id":user_id,"checkdate":checkdate},
      "type":"post",
      "async":"false"
      }).success(function(data){
        if (data.status == 1) {
          back_to();
        } else {
          AlertMSG(data.message)
        }
      }).error(function(data){
        AlertMSG("保存失败！")
      });
  }

}

function get_photo(){
  var list = [];
  $('#img_ul').find('.delete').each(function(a){
    var data = {};
    data['file_name'] = $(this).attr("attr1");
    data['file_url'] = $(this).attr("attr2");
    list.push(data);
  })

  return JSON.stringify(list);
}


  function popup(title,url){
        var index = layer.open({
            type: 2,
            title: title,
            content: url,
            area: ['600px', '400px'],
            
        });

        
        //铺满界面
        //layer.full(index);
  }
  var AlertMSG=function(msg){
    AMUI.dialog.alert({ title: '错误提示', content: msg, onConfirm: function() { console.log('close'); } });
  }

</script>
</body>

</html>